package com.ssy.lingxi.member.merchant.serviceimpl.mobile;

import com.ssy.lingxi.common.constant.manage.ShopEnvironmentEnum;
import com.ssy.lingxi.common.response.Wrapper;
import com.ssy.lingxi.member.merchant.model.constant.MemberRegisterSourceEnum;
import com.ssy.lingxi.member.merchant.model.vo.basic.request.*;
import com.ssy.lingxi.member.merchant.model.vo.basic.response.CountryAreaCodeNameVO;
import com.ssy.lingxi.member.merchant.model.vo.basic.response.CountryCodeVO;
import com.ssy.lingxi.member.merchant.model.vo.basic.response.MemberConfigGroupVO;
import com.ssy.lingxi.member.merchant.model.vo.login.request.MobileRegisterVO;
import com.ssy.lingxi.member.merchant.model.vo.login.request.ResetPasswordByEmailCodeVO;
import com.ssy.lingxi.member.merchant.model.vo.login.request.ResetPasswordBySmsCodeVO;
import com.ssy.lingxi.member.merchant.model.vo.login.response.MemberRegisterResultVO;
import com.ssy.lingxi.member.merchant.model.vo.login.response.MemberRegisterTypeMenuVO;
import com.ssy.lingxi.member.merchant.service.base.IBaseCountryAreaService;
import com.ssy.lingxi.member.merchant.service.base.IBaseMemberCacheService;
import com.ssy.lingxi.member.merchant.service.base.IBaseRegisterService;
import com.ssy.lingxi.member.merchant.service.mobile.IMobileRegisterService;
import com.ssy.lingxi.member.merchant.utils.NumberUtil;
import org.springframework.http.HttpHeaders;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * App - 会员注册相关接口实现类
 * @author 万宁
 * @version 2.0.0
 * @date 2020-12-03
 */
@Service
public class MobileRegisterServiceImpl implements IMobileRegisterService {

    @Resource
    private IBaseMemberCacheService memberCacheService;

    @Resource
    private IBaseRegisterService baseRegisterService;

    @Resource
    private IBaseCountryAreaService baseCountryAreaService;

    /**
     * 获取国家代码和手机号码位数
     * @param headers Http头部信息
     * @return 查询结果
     */
    @Override
    public Wrapper<List<CountryCodeVO>> listCountryCode(HttpHeaders headers) {
        memberCacheService.checkMobileRequestHeader(headers);
        return baseRegisterService.listCountryCode();
    }

    /**
     * 检查手机号码是否被注册使用
     *
     * @param headers Http头部信息
     * @param phoneVO 手机号码
     * @return 检查结果
     */
    @Override
    public Wrapper<Void> checkPhoneRegistered(HttpHeaders headers, PhoneVO phoneVO) {
        memberCacheService.checkMobileRequestHeader(headers);
        return baseRegisterService.checkPhoneRegistered(phoneVO);
    }

    /**
     * “手机号找回密码” - 检查手机号码是否存在
     *
     * @param headers Http头部信息
     * @param phoneVO 手机号码
     * @return 检查结果
     */
    @Override
    public Wrapper<Void> checkPhoneExistsByMobile(HttpHeaders headers, PhoneVO phoneVO) {
        memberCacheService.checkMobileRequestHeader(headers);
        return baseRegisterService.checkPhoneExistsByMobile(phoneVO);
    }

    /**
     * 检查邮箱是否被注册使用
     *
     * @param headers Http头部信息
     * @param emailVO 邮箱
     * @return 检查结果
     */
    @Override
    public Wrapper<Void> checkEmailRegistered(HttpHeaders headers, EmailVO emailVO) {
        memberCacheService.checkMobileRequestHeader(headers);
        return baseRegisterService.checkEmailRegistered(emailVO);
    }

    /**
     * 发送注册时短信验证码
     *
     * @param headers Http头部信息
     * @param phoneVO 手机号码
     * @return 发送结果
     */
    @Override
    public Wrapper<Void> sendRegisterSmsCode(HttpHeaders headers, PhoneVO phoneVO) {
        memberCacheService.checkMobileRequestHeader(headers);
        return baseRegisterService.sendRegisterSmsCode(phoneVO);
    }

    /**
     * 发送手机号找回密码时的短信验证码
     *
     * @param headers Http头部信息
     * @param phoneVO 手机号码
     * @return 发送结果
     */
    @Override
    public Wrapper<Void> sendResetPasswordSmsCode(HttpHeaders headers, PhoneVO phoneVO) {
        memberCacheService.checkMobileRequestHeader(headers);
        return baseRegisterService.sendMobileResetPasswordSmsCode(phoneVO);
    }

    /**
     * 校验手机号找回密码时的短信验证码是否正确
     *
     * @param headers    Http头部信息
     * @param phoneSmsVO 手机号码、验证码
     * @return 发送结果
     */
    @Override
    public Wrapper<Void> checkResetPasswordSmsCode(HttpHeaders headers, PhoneSmsVO phoneSmsVO) {
        memberCacheService.checkMobileRequestHeader(headers);
        return baseRegisterService.checkResetPasswordSmsCode(phoneSmsVO);
    }

    /**
     * 根据短信验证码重设密码
     *
     * @param headers Http头部信息
     * @param codeVO  接口参数
     * @return 操作结果
     */
    @Override
    public Wrapper<Void> resetPasswordBySmsCode(HttpHeaders headers, ResetPasswordBySmsCodeVO codeVO) {
        memberCacheService.checkMobileRequestHeader(headers);
        return baseRegisterService.resetPasswordBySmsCode(codeVO);
    }

    /**
     * 发送邮箱找回密码时的邮件
     *
     * @param headers Http头部信息
     * @param emailVO 邮箱地址
     * @return 发送结果
     */
    @Override
    public Wrapper<Void> sendResetPasswordEmail(HttpHeaders headers, EmailVO emailVO) {
        memberCacheService.checkMobileRequestHeader(headers);
        return baseRegisterService.sendResetPasswordEmail(emailVO, true);
    }

    /**
     * 发送邮箱找回密码时的邮件
     *
     * @param headers Http头部信息
     * @param emailVO 邮箱地址
     * @return 发送结果
     */
    @Override
    public Wrapper<Void> checkResetPasswordEmailCode(HttpHeaders headers, EmailSmsVO emailVO) {
        memberCacheService.checkMobileRequestHeader(headers);
        return baseRegisterService.checkResetPasswordEmailCode(emailVO);
    }

    /**
     * 根据邮箱验证码重设密码
     *
     * @param headers Http头部信息
     * @param codeVO  接口参数
     * @return 操作结果
     */
    @Override
    public Wrapper<Void> resetPasswordByEmailCode(HttpHeaders headers, ResetPasswordByEmailCodeVO codeVO) {
        memberCacheService.checkMobileRequestHeader(headers);
        return baseRegisterService.resetPasswordByEmailCode(codeVO);
    }

    /**
     * 获取会员注册页面-会员类型、商业类型页面内容（第二页）
     * @param headers Http头部信息
     * @return 操作结果
     */
    @Override
    public Wrapper<List<MemberRegisterTypeMenuVO>> getRegisterTypePageContent(HttpHeaders headers) {
        memberCacheService.checkMobileRequestHeader(headers);
        return baseRegisterService.getMobileRegisterTypePageContent();
    }

    /**
     * 获取会员注册页面-详细信息页面内容（第三页）
     * @param headers  Http头部信息
     * @param detailVO 接口参数
     * @return 注册第三页的内容
     */
    @Override
    public Wrapper<List<MemberConfigGroupVO>> getRegisterDetailPageContent(HttpHeaders headers, MemberDetailVO detailVO) {
        memberCacheService.checkMobileRequestHeader(headers);
        return baseRegisterService.getRegisterDetailPageContent(detailVO);
    }

    /**
     * 查询省列表
     *
     * @param headers Http头部信息
     * @return 查询结果
     */
    @Override
    public Wrapper<List<CountryAreaCodeNameVO>> listProvince(HttpHeaders headers) {
        memberCacheService.checkMobileRequestHeader(headers);
        return baseCountryAreaService.listProvince();
    }

    /**
     * 根据省编码，查询市列表
     *
     * @param headers Http头部信息
     * @param codeVO  接口参数
     * @return 查询结果
     */
    @Override
    public Wrapper<List<CountryAreaCodeNameVO>> listCitiesByProvinceCode(HttpHeaders headers, CountryAreaCodeVO codeVO) {
        memberCacheService.checkMobileRequestHeader(headers);
        return baseCountryAreaService.listCitiesByProvinceCode(codeVO);
    }

    /**
     * 根据市编码，查询区列表
     *
     * @param headers Http头部信息
     * @param codeVO  接口参数
     * @return 查询结果
     */
    @Override
    public Wrapper<List<CountryAreaCodeNameVO>> listDistrictsByCityCode(HttpHeaders headers, CountryAreaCodeVO codeVO) {
        memberCacheService.checkMobileRequestHeader(headers);
        return baseCountryAreaService.listDistrictsByCityCode(codeVO);
    }

    /**
     * 平台会员注册
     * @param headers    Http头部信息
     * @param registerVO 接口参数
     * @return 操作结果
     */
    @Override
    public Wrapper<MemberRegisterResultVO> registerPlatformMember(HttpHeaders headers, MobileRegisterVO registerVO) {
        memberCacheService.checkMobileRequestHeader(headers);
        MemberRegisterSourceEnum registerSourceEnum = MemberRegisterSourceEnum.parseInt(registerVO.getRegisterSource());

        String env = headers.getFirst("environment");
        Integer envCode = StringUtils.hasLength(env) ? NumberUtil.tryParseInteger(env) : 0;
        if(envCode.equals(ShopEnvironmentEnum.APPLETS.getCode())) {
           switch (registerSourceEnum) {
               case FROM_ENTERPRISE_APP_SHOP:
                   registerSourceEnum = MemberRegisterSourceEnum.FROM_ENTERPRISE_MINI_APP;
                   break;
               case FROM_CHANNEL_APP_SHOP:
                   registerSourceEnum = MemberRegisterSourceEnum.FROM_CHANNEL_MINI_APP;
                   break;
               case FROM_CHANNEL_APP_PROPERTY_SHOP:
                   registerSourceEnum = MemberRegisterSourceEnum.FROM_CHANNEL_PROPERTY_MINI_APP;
                   break;
               case FROM_ENTERPRISE_APP_SCORE_SHOP:
                   registerSourceEnum = MemberRegisterSourceEnum.FROM_ENTERPRISE_SCORE_MINI_APP;
                   break;
               case FROM_CHANNEL_APP_SCORE_SHOP:
                   registerSourceEnum = MemberRegisterSourceEnum.FROM_CHANNEL_SCORE_MINI_APP;
                   break;
               default:
                   break;
           }
        }

        return baseRegisterService.registerPlatformMember(registerSourceEnum,registerVO.getSmsCode(), registerVO.getCountryCode(),registerVO.getPhone(),registerVO.getPassword(),registerVO.getEmail(),registerVO.getMemberTypeId(),registerVO.getBusinessTypeId(),registerVO.getDetail());
    }
}
